package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.entity.*;
import com.softer.wxzj.mapper.*;
import com.softer.wxzj.service.AccTransactionLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * AccBusiVouchLinkServiceImpl class
 *
 * @author wxr
 * @date 2019/7/2
 */
@Slf4j
@Service
public class AccTransactionLogServiceImpl implements AccTransactionLogService {
    @Autowired
    private AccTransactionLogMapper accTransactionLogMapper;
    @Autowired
    private BaseHouseInfoMapper baseHouseInfoMapper;
    @Autowired
    private BaseOwnerInfoMapper baseOwnerInfoMapper;
    @Autowired
    private SysOperatorMapper sysOperatorMapper;

    @Override
    public PageInfo<AccTransactionLog> selectLog(Page page, String houseId) {
        //分页合计金额
        BigDecimal pageMoney = BigDecimal.ZERO;
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<AccTransactionLog> list = accTransactionLogMapper.selectbus(houseId);
        for (AccTransactionLog accTransactionLog : list) {
            if (StringUtils.isNotBlank(accTransactionLog.getTallyOper())) {
                accTransactionLog.setCreater(sysOperatorMapper.selectName(accTransactionLog.getTallyOper()).getOperName());
            }
            if (!accTransactionLog.getBusiType().equals("311")) {
                pageMoney = pageMoney.subtract(accTransactionLog.getPayAmount());
            } else {
                pageMoney = pageMoney.add(accTransactionLog.getPayAmount());
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = null;
            try {
                date1 = sdf.parse(accTransactionLog.getTallyDate());
            } catch (ParseException e) {
                log.error("ParseException错误信息:()", e.getMessage(), e);
            }
            String datestr = sdf.format(date1);
            accTransactionLog.setTallyDate(datestr);
            switch (accTransactionLog.getBusiType()) {
                case "310":
                    if (StringUtils.isBlank(accTransactionLog.getBusiNo())) {
                        accTransactionLog.setBusiTypeName("(过户)退款业务");
                        accTransactionLog.setCreater(sysOperatorMapper.selectName(accTransactionLog.getCreater()).getOperName());

                    } else {
                        accTransactionLog.setBusiTypeName("退款业务");

                    }
                    break;
                case "312":
                    accTransactionLog.setBusiTypeName("支用业务");
                    break;
                case "311":
                    if (StringUtils.isBlank(accTransactionLog.getBusiNo())) {
                        accTransactionLog.setBusiTypeName("(过户)交存业务");
                        accTransactionLog.setCreater(sysOperatorMapper.selectName(accTransactionLog.getCreater()).getOperName());

                    } else {
                        accTransactionLog.setBusiTypeName("交存业务");
                    }
                    break;
                default:
                    accTransactionLog.setBusiTypeName("结息业务");
                    accTransactionLog.setCreater(sysOperatorMapper.selectName(accTransactionLog.getCreater()).getOperName());
            }
            BaseHouseInfo baseHouseInfo = baseHouseInfoMapper.getInfo(accTransactionLog.getHouseId());
            accTransactionLog.setHouseSite(baseHouseInfo.getHouseSite());
            if (StringUtils.isNotBlank(accTransactionLog.getOwnerId())) {
                accTransactionLog.setName(baseOwnerInfoMapper.getInfoById(accTransactionLog.getOwnerId()).getName());
            }
        }
        //总合计
        AccTransactionLog accTransactionLog = new AccTransactionLog();
        //分页合计
        AccTransactionLog pagAccTransactionLog = new AccTransactionLog();
        pagAccTransactionLog.setPayAmount(pageMoney);
        BigDecimal allMoney = accTransactionLogMapper.selectAllMoney(houseId);
        accTransactionLog.setPayAmount(allMoney);
        list.add(pagAccTransactionLog);
        list.add(accTransactionLog);

        if (list == null || list.size() < 3) {
            return new PageInfo<>();
        }
        PageInfo<AccTransactionLog> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }

    private BigDecimal accTransactionLogsAdd(List<AccTransactionLog> acc, BigDecimal b) {
        for (AccTransactionLog a : acc) {
            if (StringUtils.isNotBlank(a.getSubRegionName())) {
                a.setBuildName(a.getBuildName() + a.getSubRegionName());
            }
            if (null != a.getPayAmount()) {
                b = b.add(a.getPayAmount());
            }
        }
        return b;
    }

    @Override
    public PageInfo<AccTransactionLog> selectLogByBusiNo(Page page, String busiNo, String buildName, String fuzzy) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<AccTransactionLog> acc = accTransactionLogMapper.selectLogByBusiNo(busiNo, buildName, fuzzy);
        BigDecimal b = new BigDecimal(0);
        b = accTransactionLogsAdd(acc, b);

        AccTransactionLog a = new AccTransactionLog();
        a.setPayAmount(b);
        AccTransactionLog accAll = accTransactionLogMapper.selectLogByBusiNoAll(busiNo, buildName);
        acc.add(a);
        acc.add(accAll);
        PageInfo<AccTransactionLog> pageInfo = new PageInfo<>(acc);
        return pageInfo;
    }

    @Override
    public List<AccTransactionLog> selectLogByBusiNo(String busiNo) {
        return accTransactionLogMapper.selectFullByBusNo(busiNo);
    }

    @Override
    public List<AccTransactionLog> selectLogByBusiNoCell(AccTransactionLog accTransactionLog) {
        List<AccTransactionLog> acc = new ArrayList<>();
        if (("yes").equals(accTransactionLog.getThisPage())) {
            PageHelper.startPage(accTransactionLog.getPageNum(), accTransactionLog.getPageSize());
            acc = accTransactionLogMapper.selectLogByBusiNo(accTransactionLog.getBusiNo(), accTransactionLog.getBuildName(), accTransactionLog.getFuzzy());
        } else {
            accTransactionLog.setPageNum(0);
            accTransactionLog.setPageSize(0);
            acc = accTransactionLogMapper.selectLogByBusiNo(accTransactionLog.getBusiNo(), accTransactionLog.getBuildName(), accTransactionLog.getFuzzy());
        }
        BigDecimal b = new BigDecimal(0);
        b = accTransactionLogsAdd(acc, b);
        AccTransactionLog a = new AccTransactionLog();
        a.setPayAmount(b);
        acc.add(a);
        return acc;
    }

}
